package com.dmcb.trade.business.crawlers.author;

import com.dmcb.common.business.constants.CommonConstant;
import com.dmcb.common.business.services.BaseService;
import com.dmcb.common.business.utils.WebUtil;
import com.dmcb.common.web.conversion.JsonResult;
import com.dmcb.common.business.utils.WebClientUtil;
import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.entities.Article;
import com.dmcb.trade.business.mappers.ArticleCrawlMapper;
import com.dmcb.trade.controller.ArticleCrawlerController;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 鸵鸟电台文章自动导入监测
 * Created by 黑米 on 2017/1/4.
 */
@Service
public class TuoNiaoCrawler  extends BaseService {
    Logger logger = LoggerFactory.getLogger(TuoNiaoCrawler.class);
    @Autowired
    private ArticleCrawlMapper articleCrawlMapper;
    @Autowired
    private ArticleCrawlerController articleCrawlerController;
    @Autowired
    private WebClientUtil webClientUtil;


    public JsonResult crawl() {
        List<String> typeList = new ArrayList<String>();
        typeList.add("国内融资发布");
        typeList.add("鸵鸟干货");
        typeList.add("海外创投");
        typeList.add("大公司");
        typeList.add("投资人说");
        typeList.add("生活消费");
        typeList.add("智能科技");
        typeList.add("区块链");
        typeList.add("工具软件");
        typeList.add("企业服务");
        typeList.add("苹果");
        typeList.add("软件");
        typeList.add("其他");
        typeList.add("移动应用");
        // 爬取文章

        int totalAll = 0;
        Document doc;
        for(String typeLink : typeList ){
            int page = 1;
            String src = "http://tuoniao.fm/?route=information/article/tag&tag="+ WebUtil.URLEncode(typeLink)+"&page="+page;
            doc = Jsoup.parse(webClientUtil.getPage(src).asXml());
            while (page <= CrawlConstant.CRAWL_PAGE_TOTAL){
                JsonResult result = iterate(3077,doc);
                if (!result.isSuccess()) {
                    return result;
                }
                totalAll += result.getInt();
                if (result.getInt() < 1) {
                    break;
                }
                page++;
                 src = "http://tuoniao.fm/?route=information/article/tag&tag="+WebUtil.URLEncode(typeLink)+"&page="+page;
                doc = Jsoup.parse(webClientUtil.getPage(src).asXml());
            }
        }
        return totalAll == 0 ? error(CrawlConstant.MSG_WARN_NO_UPDATE) : success(totalAll);

    }

    /**
     * 迭代解析、保存文章
     *
     * @return 成功保存数量
     */
    protected JsonResult iterate(Integer authorId, Document doc) {
        Elements elements = doc.select("li.article-card");
        if (elements.size() == 0) {
            return success(0);
        }
        int succ = 0;
        int exist = 0;
        for (Element element : elements) {
            // 获取标题、链接元素
            Element ele = element.select("a.transition").first();
            if (ele != null) {
                try {
                    // 校验作者、标题是否已存在
                    Article article = articleCrawlMapper.queryByAuthorTitle(authorId, ele.text().trim());
                    if (article != null) {
                        exist++;
                        if (exist >= 6 && succ == 0) {
                            return success(0);
                        }
                        continue;
                    }
                    String href = ele.absUrl("href");
                    JsonResult result = articleCrawlerController.crawlMonitor(authorId, href);
                    succ += result.isSuccess() ? 1 : 0;
                    Thread.sleep(1000);
                } catch (Exception e) {
                    logger.error("Parser.parse error,{}", ele, e);
                    return error(CommonConstant.ABNORMAL_MSG);
                }
            }
        }
        return success(succ);
    }
}


